package ro.jproject.imc.view;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import ro.jproject.imc.model.CnpVerification;
import ro.jproject.imc.model.DataBaseConnection;
import javax.swing.JTextField;
import javax.swing.JComboBox;




@SuppressWarnings("serial")
public class AddProfessorGUI extends JFrame {

	private JFrame frame;
	private JPanel mainPanel;
	private JButton save;
	private JLabel lastNameLabel;
	private JLabel firstNameLabel;
	private JLabel lblTitle;
	private JLabel lblCnp;
	private JButton buttonBack;
	private String saveFirstName;
	private String saveLastName;
	private String saveTitle;
	private String saveField;
	private String cnp;
	private JTextField textFieldFirstName;
	private JTextField textFieldLastName;
	private JTextField textFieldTitle;
	private JTextField textFieldCnp;
	private JTextField textField;
	public static JComboBox<String> comboBoxFaculty = new JComboBox<String>();
	
	/**
	 * Create the frame.
	 */
	
	public AddProfessorGUI() {
		setTitle("Professor Registration");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(500, 200, 385, 371);
		
		frame = new JFrame();
		mainPanel = new JPanel();
		save = new JButton("Save");
		save.setBounds(27, 276, 104, 32);
		firstNameLabel = new JLabel("First Name");
		firstNameLabel.setBounds(38, 29, 69, 14);
		lastNameLabel = new JLabel("Last Name");
		lastNameLabel.setBounds(38, 59, 69, 14);
		buttonBack = new JButton("Back");
		buttonBack.setBounds(223, 276, 104, 32);
		
		setContentPane(mainPanel);
		//frame.getContentPane().add(mainPanel);
		mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		mainPanel.setLayout(null);
		mainPanel.add(save);
		mainPanel.add(firstNameLabel);
		mainPanel.add(lastNameLabel);
		
		//SaveButtonAction saveButton = new SaveButtonAction();
		//saveButton.setSaveButtonAction();
		
		//ExitButtonAction exit = new ExitButtonAction();
		//exit.exitButtonAction();
		
		buttonsAction();
		mainPanel.add(buttonBack);
		
		textFieldFirstName = new JTextField();
		textFieldFirstName.setBounds(113, 26, 214, 20);
		mainPanel.add(textFieldFirstName);
		textFieldFirstName.setColumns(10);
		
		textFieldLastName = new JTextField();
		textFieldLastName.setBounds(113, 56, 214, 20);
		mainPanel.add(textFieldLastName);
		textFieldLastName.setColumns(10);
		
		textFieldTitle = new JTextField();
		textFieldTitle.setBounds(113, 116, 214, 20);
		mainPanel.add(textFieldTitle);
		textFieldTitle.setColumns(10);
		
		lblTitle = new JLabel("Title");
		lblTitle.setBounds(38, 119, 46, 14);
		mainPanel.add(lblTitle);
		
		textFieldCnp = new JTextField();
		textFieldCnp.setBounds(113, 86, 214, 20);
		mainPanel.add(textFieldCnp);
		textFieldCnp.setColumns(10);
		
		lblCnp = new JLabel("CNP");
		lblCnp.setBounds(38, 89, 32, 17);
		mainPanel.add(lblCnp);
		
		textField = new JTextField();
		textField.setBounds(113, 146, 214, 20);
		mainPanel.add(textField);
		textField.setColumns(10);
		
		JLabel lblField = new JLabel("Field");
		lblField.setBounds(38, 149, 32, 14);
		mainPanel.add(lblField);
		
		
		
	    
	    
	    DataBaseConnection dataBase = new DataBaseConnection();
	    dataBase.connected();
	    try {
			dataBase.queryShowUniversityDataBase();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		comboBoxFaculty.setBounds(113, 177, 214, 20);
		mainPanel.add(comboBoxFaculty);
		
		JLabel lblUniversity = new JLabel("University");
		lblUniversity.setBounds(38, 180, 69, 14);
		mainPanel.add(lblUniversity);
		
	}

	void saveListener(ActionListener s) {
        save.addActionListener(s);
    }
	
	public void buttonsAction() {
		//save button
		save.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0){

				saveFirstName = textFieldFirstName.getText();
				saveLastName = textFieldLastName.getText();
				saveTitle = textFieldTitle.getText();
				saveField = textField.getText();
				cnp = textFieldCnp.getText();
				
				CnpVerification cnpValid = new CnpVerification();
				cnpValid.cnpVerif(cnp);
				
				DataBaseConnection connection = new DataBaseConnection();
				
				try {
					connection.connected();
					connection.insertQueryProfesors(saveFirstName, saveLastName, cnp, saveTitle, saveField);
					
					JOptionPane.showMessageDialog(frame,"Registration saved!");
					
					textFieldFirstName.setText("");
					textFieldFirstName.setText("");
					textFieldTitle.setText("");
					textField.setText("");
					textFieldCnp.setText("");
					
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
			}
		});
		//back button
		buttonBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();		
				Ums frame = new Ums();
				frame.setVisible(true);
			}
		});
	}
}
